//
//  ReplyMessBoardListVC.m
//  SelfDrivingTravelling-IOS
//
//  Created by Teplot_01 on 16/10/19.
//  Copyright © 2016年 Teplot_01. All rights reserved.
//

#import "ReplyMessBoardListVC.h"
#import "SetUpMessBoardVC.h"
#import "AnswerMessBoardVC.h"
@interface ReplyMessBoardListVC ()<UITableViewDelegate,UITableViewDataSource>

@end

@implementation ReplyMessBoardListVC
{
    UITableView *_tableView;
    NSArray *_responseArr;
}
- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = White_Color;
    self.title = @"留言回复列表";
    [self requestForReplyMessBoardList];
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc]initWithTitle:@"留言" style:UIBarButtonItemStylePlain target:self action:@selector(setUpMessBoard)];
}
- (void)initTableView{
    _tableView = [[UITableView alloc]initWithFrame:self.view.frame];
    _tableView.delegate = self;
    _tableView.dataSource = self;
    [self.view addSubview:_tableView];
}
#pragma - mark RequestForMessBoardList
- (void)requestForReplyMessBoardList{
    //参数
    NSDictionary *parameters = @{
                                 @"bbsID": @"13",
                                 @"pageNum": @"1",
                                 @"pageSize": @"20",
                                 };
    
    AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
    manager.requestSerializer=[AFHTTPRequestSerializer serializer];
    manager.responseSerializer = [AFHTTPResponseSerializer serializer];
    
    [manager POST:@"http://115.28.152.130/tour/v1/bbs/replyList.action" parameters:parameters success:^(NSURLSessionDataTask *operation, id responseObject) {
        NSDictionary *dic = [NSJSONSerialization JSONObjectWithData:responseObject options:NSJSONReadingAllowFragments error:nil];
        NSLog(@"%@>>>>",dic);
        NSString *error = dic[@"code"];
        if (![dic[@"code"] isEqualToString:@"0000"]) {
            [SVProgressHUD showErrorWithStatus:[NSString errorStringWithCode:error]];
        }else{
            //获取成功
            _responseArr = dic[@"list"];
            [SVProgressHUD showSuccessWithStatus:@"获取留言列表成功"];
            [_tableView reloadData];
        }
        
    } failure:^(NSURLSessionDataTask *operation, NSError *error) {
        [SVProgressHUD showErrorWithStatus:@"获取留言列表失败"];
    }];
}
#pragma - mark UITableViewDelegate
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return _responseArr.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    UITableViewCell *cell = [[UITableViewCell alloc]init];
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    UIImageView *icon = [[UIImageView alloc]initWithFrame:CGRectMake(10, 3, 50, 50)];
    icon.image = [UIImage imageNamed:@"head_portrait"];
    [cell.contentView addSubview:icon];
    UILabel *nameLb = [[UILabel alloc]initWithFrame:CGRectMake(70, 3, 100, 15)];
    nameLb.text = @"月亮之上";
    nameLb.font = [UIFont systemFontOfSize:13];
    [cell.contentView addSubview:nameLb];
    UILabel *timeLb = [[UILabel alloc]initWithFrame:CGRectMake(70, 35, 150, 15)];
    NSString *timeStr = _responseArr[indexPath.row][@"createTime"];
    timeStr = [timeStr substringToIndex:19];
    timeLb.text = timeStr;
    timeLb.font = [UIFont systemFontOfSize:13];
    timeLb.textColor = Gray_Color;
    [cell.contentView addSubview:timeLb];
//    UIButton *answerBtn = [[UIButton alloc]initWithFrame:CGRectMake(KScreenWidth - 80, 10, 30, 15)];
//    [answerBtn setTitle:@"回复" forState:UIControlStateNormal];
//    [answerBtn addTarget:self action:@selector(answerBtnAction) forControlEvents:UIControlEventTouchUpInside];
//    [answerBtn setTitleColor:Orange_Color forState:UIControlStateNormal];
//    answerBtn.titleLabel.font = [UIFont systemFontOfSize:12];
//    [cell.contentView addSubview:answerBtn];
    UILabel *contentLb = [[UILabel alloc]initWithFrame:CGRectMake(10, 55, KScreenWidth - 20, 40)];
    contentLb.numberOfLines = 0;
    contentLb.font = [UIFont systemFontOfSize:11];
    contentLb.textColor = Gray_Color;
    contentLb.text = _responseArr[indexPath.row][@"content"];
    [cell.contentView addSubview:contentLb];
    return cell;
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 100;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    AnswerMessBoardVC *vc = [[AnswerMessBoardVC alloc]init];
    vc.originalID = _responseArr[indexPath.row][@"id"];
    [self.navigationController pushViewController:vc animated:YES];
}

- (void)setUpMessBoard{
    SetUpMessBoardVC *vc = [[SetUpMessBoardVC alloc]init];
    vc.clubId = self.clubId;
    [self.navigationController pushViewController:vc animated:YES];
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
